﻿using System;
using UnityEngine;
using System.Collections.Generic;
using System.Text;

namespace Fsm
{

	public class FsmControllerBaseMono<T> : MonoBehaviour ,IFsmController<T>
	{
		protected FsmStateMonoBase<T> currentState;
		
		public IFsmState<T> GetCurrentState()
		{
			return currentState;
		}
		
		public virtual void InitState()
		{
			allStates = new Dictionary<T, FsmStateMonoBase<T>>();
			//add state into the dictionary
			//allStates[T] = new FsmStateBase<T>(T,this);
		}
		
		protected Dictionary<T, FsmStateMonoBase<T>> allStates;
		
		public IFsmState<T> GetState(T stateType)
		{
			return allStates[stateType];
		}
		
		public T GetCurrrentStateType()
		{
			if(currentState != null)
			{
				return currentState.GetStateType();
			}
			else
			{
				return default(T);
			}
		}
		
		public virtual void SwitchState(T stateType)
		{
			if(currentState != null)
			{
				currentState.Leave();
			}
			currentState = allStates[stateType];
            currentState.Enter();
		}
		
		public virtual void UpdateState()
		{
			if(currentState != null)
			{
				currentState.Update();
			}
		}
		
	}
}
